﻿using System;
using System.Collections;
using System.Collections.Generic;

using MVCFramework.Models;


namespace MVCFramework.View
{
    [CLSCompliant (false )]
    public interface IViewSelector
    {
         ArrayList getSelectedObjects(uint[] objectArray);
    }

    [CLSCompliant(false)]
    public abstract class ViewSelector: IViewSelector, IComparer  
    {
        IModel _model;

        public ViewSelector(IModel model)
        {
            this._model = model;
        }

        protected ArrayList selecetdObjects;

        public ArrayList getSelectedObjects(uint[] objectArray)
        {
            if ( objectArray == null ) return null;
            selecetdObjects = new ArrayList();
            if (objectArray.Length >= 4)
                for (int loop = 0; loop < (objectArray.Length / 4); loop++)
                    if (!(addObjectToList((int)objectArray[loop * 4 + 3]) && objectArray[loop * 4 + 3] != 0)) ;

                return  DoGetReturnData(this);
         }

        protected virtual ArrayList DoGetReturnData(IComparer viewSelector)
        {
            return selecetdObjects;
        }

        private bool addObjectToList(int id)
        {
            int maxid = _model.MaxId;
            object current = _model.GetObject(id);//ModelIDManager.Instance.GetObjectById(id);
            if (current != null)
            {
                selecetdObjects.Add(current);
                return true;
            }
            else if (SnapManager.Instance.SnapTable.ContainsKey(id - maxid))
            {
                current = SnapManager.Instance.SnapTable[id - maxid];
                if (current != null)
                {
                    selecetdObjects.Add(current);
                    return true;
                }
            }
                 /** */
            return false;
        }

        #region IComparer Members

        public abstract int Compare(object x, object y);

        #endregion
    }
    //malakies  IComparer , sroting selection 
    public class DefultViewSelector : ViewSelector
    {
        public DefultViewSelector(IModel model) : base(model) { }

        protected override ArrayList DoGetReturnData(IComparer viewSelector)
        {
            return selecetdObjects;
        }

        public override int Compare(object x, object y)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
